package com.qiangesoft.blog.file.core;

import com.qiangesoft.blog.file.core.minio.MinioTemplate;
import com.qiangesoft.blog.file.model.FileInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件minio对象存储
 *
 * @author qiange
 * @date 2023-04-26
 */
@Component("FILE_MINIO")
public class MinioFileStoreImpl implements FileStoreStrategy {

    @Autowired
    private MinioTemplate minioTemplate;

    @Override
    public FileInfoVo uploadFile(MultipartFile file) throws Exception {
        return minioTemplate.uploadFile(file);
    }

    @Override
    public FileInfoVo uploadFile(MultipartFile file, String path) throws Exception {
        return minioTemplate.uploadFile(file, path);
    }

    @Override
    public FileInfoVo uploadFile(InputStream inputStream, String contentType) throws Exception {
        return minioTemplate.uploadFile(inputStream, contentType);
    }

    @Override
    public List<FileInfoVo> batchUploadFile(MultipartFile[] files, String path) throws Exception {
        List<FileInfoVo> fileInfoVos = new ArrayList<>();
        for (MultipartFile file : files) {
            FileInfoVo fileInfoVo = minioTemplate.uploadFile(file);
            fileInfoVos.add(fileInfoVo);
        }
        return fileInfoVos;
    }

    @Override
    public FileInfoVo uploadFile(InputStream inputStream, String contentType, String path) throws Exception {
        return minioTemplate.uploadFile(inputStream, contentType, path);
    }

    @Override
    public List<FileInfoVo> batchUploadFile(MultipartFile[] files) throws Exception {
        List<FileInfoVo> fileInfoVos = new ArrayList<>();
        for (MultipartFile file : files) {
            FileInfoVo fileInfoVo = minioTemplate.uploadFile(file);
            fileInfoVos.add(fileInfoVo);
        }
        return fileInfoVos;
    }

    @Override
    public void downloadFile(String key, HttpServletRequest request, HttpServletResponse response) throws Exception {
        minioTemplate.download(key, request, response);
    }

    @Override
    public void deleteFile(String key) throws Exception {
        minioTemplate.removeObject(key);
    }
}
